<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>中心医院医疗服务系统</title>
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/default/easyui.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/icon.css"/>">
<link rel="stylesheet" type="text/css" href="<c:url value="/css/themes/yzr_style.css"/>">
<link rel="stylesheet" type="text/css"
	href="<c:url value="/css/themes/lmb_style.css"/>">
<script type="text/javascript" src="<c:url value="/js/jquery.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/jquery.easyui.min.js"/>"></script>
<script type="text/javascript"
	src="<c:url value="/js/easyui-lang-zh_CN.js"/>"></script>
<style type="text/css">
* {
	font-size: 12px;
	padding:0px;
	margin:0px;
}
</style>
</head>
<body class="easyui-layout">
	<script src="<c:url value="/js/jquery-migrate-1.1.0.min.js"/>"></script>
	<script type="text/javascript" src="<c:url value="/js/jquery.jqprint-0.3.js"/>"></script>
	<script type="text/javascript">

	var editIndex = undefined;
	//结束正在编辑的行
	function endEditing(){
		if (editIndex == undefined){return true}
		if ($('#lmb_hc_xq').datagrid('validateRow', editIndex)){
			$('#lmb_hc_xq').datagrid('endEdit', editIndex);	
			
			return true;
		} else {
			return false;
		}
	}
	
	//事件：单击单元格的事件
	function onClickCell(index, field){
		if (editIndex != index){
			if (endEditing()){
				$('#lmb_hc_xq').datagrid('selectRow', index)
						.datagrid('beginEdit', index);
				var ed = $('#lmb_hc_xq').datagrid('getEditor', {index:index,field:field});
				if (ed){
					($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
				}
				editIndex = index;
			} else {
				setTimeout(function(){
					$('#lmb_hc_xq').datagrid('selectRow', editIndex);
				},0);
			}
		}
	}
	//结束编辑，同步数据
	function onEndEdit(index, row){
		var ed = $(this).datagrid('getEditor', {
			index: index,
			field: 'cpdName'
		});
		var ed2 = $(this).datagrid('getEditor', {
			index: index,
			field: 'cpdCId'
		});
		row['cpdCId'] = $(ed2.target).textbox('getText');	
		row['cpdName'] = $(ed.target).combobox('getText');
		
		var ed1 = $(this).datagrid('getEditor', {
			index: index,
			field: 'gname'
		});
		var ed3 = $(this).datagrid('getEditor', {
			index: index,
			field: 'cpdSupplier'
		});
		row['cpdSupplier'] =$(ed3.target).textbox('getText'); 		
		row['gname'] = $(ed1.target).combobox('getText');	

		//自动算出金额
		
		var lmb_price = $(this).datagrid('getEditor', {
			index: index,
			field: 'cpdPrice'
		});				
		var lmb_number = $(this).datagrid('getEditor', {
			index: index,
			field: 'cpdNum'
		});	
		row['zj'] = $(lmb_price.target).textbox('getValue')*$(lmb_number.target).textbox('getValue');
	}
	
	//应用
	function accept(){
		if (endEditing()){			
			endEditing();
			
		}
	}
	
	//撤消
	function reject(){
		$('#lmb_hc_xq').datagrid('rejectChanges');
		editIndex = undefined;
	}

	//获取数据
	function getChanges(){
		var rows = $('#lmb_hc_xq').datagrid('getChanges');
		alert(rows.length+' rows are changed!');
	}
	//采购计划单号改变时触发
	$("#lmb-coupur-cg").combogrid({onChange: function(newValue, oldValue){				
		$("#lmb_hc_xq").datagrid({
			url:'to_queryCurPurPlanDetailsByPage',
			width: '100%',
			queryParams:{
				cpPlanId: newValue,
				page:1, 
    			rows:10
			}
		});		
	}});
	//提交采购计划
	function saveHc(){
		//将datagrid中的数据行保存到隐式表单域中
		//1、获取新增的数据行
		var rows = $('#lmb_hc_xq').datagrid('getChanges');
		console.log(JSON.stringify(rows));
		
		//为每一行生成对应的隐藏表单域
		//cpdCId，cpdName，ph，cpdNorms，dj，cpdNum，zj，scrq，yxrq，gno，gname
		for(var i = 0; i < rows.length; i ++){
			generateHidden(rows[i], "CId", i);
			generateHidden(rows[i], "Name", i);//1
			generateHidden(rows[i], "BatchNum", i);
			generateHidden(rows[i], "Norms", i);//2
			generateHidden(rows[i], "Price", i);
			generateHidden(rows[i], "Num", i);
			generateHidden(rows[i], "ProductDate", i);
			generateHidden(rows[i], "ValidTill", i);
			generateHidden(rows[i], "Supplier", i);//3
		}
		
		//提交表单
		var hc = $("#lmb_hc");
		hc.form("submit", {
			success: function(v){
				$.messager.alert('提示', "提交成功");
				$(":hidden[x='y']").remove();
				//关闭窗口
				$('#lmb_win_hc-add').window('close');
				//重新加载数据lmb_hc_tab
				$("#lmb_hc_tab").datagrid("reload");
				$(this).form("clear");
				reject();	
			}				
		});	
		
	
	}
		
	function generateHidden(row, name, i){
		var lmb_hc = $("#lmb_hc");
		//type='hidden'
		$("<input type='hidden' x='y'>")
			.attr("name", "purchaseDetail[" + i + ']' + '.pd' + name)
			.val(row['cpd' + name])
			.appendTo(lmb_hc);
	} 
	
	//日期转换
	function lmb_todate_1(value){
        var date = new Date(value);  
        return date.toLocaleDateString();
    }
  
	$(function(){
		
		//打开查询采购详情的窗口
		$("#hc-xq").linkbutton({onClick: function(){
			var rows = $("#lmb_hc_tab").datagrid("getSelections");
			if(rows.length == 1){
				$('#lmb-hcDet-window').window("open");
				var cpId = rows[0].cpId;
				$.getJSON("to_lmb_findCouPurById", {"cpId": cpId}, function(data){
					$("#lmb-hcDet-cgdh").linkbutton({text: data.cpId});
					$("#lmb-hcDet-jhdh").linkbutton({text: data.cpPlId});
					$("#lmb-hcDet-cgy").linkbutton({text: data.name});
					$("#lmb-hcDet-cgsj").linkbutton({text: lmb_todate_1(data.cpTime)});
				});	
				
				 //根据id查看详情	
				 $("#lmb-hcxDet-dg").datagrid({
					url:'to_lmb_findPurchaseDetailVo',
					width: '100%',
					queryParams:{					
		    			pdcpId: cpId
					}
				});
			}else{
				$.messager.alert("提示","只能选择一条记录，您目前选择了"+ rows.length + "条!");
			}
			
		}});
		
		$("#hc-add").linkbutton({onClick:function(){						
			$("#lmb_win_hc-add").window("open");
			$("#lmb_hc").form("clear");
			$("#lmb_hc_xq_tab").form("clear");
			$('#lmb_hc_xq').datagrid('loadData', { total: 0, rows: [] }); 
		}});	
		
		$('#lmb_hc-search').searchbox({
		    searcher:function(value,name){
		    	if(value == ''){
		    		$.messager.alert("提示", "请输入查询条件！");
		    	}else{
		    		$("#lmb_hc_tab").datagrid({	    				    		
			    		method: 'post', 
			    		url:'to_lmb_findLmbCouPurVoByIf',
			    		queryParams: {
			    			name: name,
			    			shuxing: value,			    			
			    			page:1, 
			    			rows:10
			    	}}); 		
		    	}		    	
		    }
		
		}); 
		
		//打印
		/*function printdivHC(printpage){ 
			//获得当前页面body里面的内容
			var bodyHtml = document.body.innerHTML;
			//把打印的内容填充
			document.body.innerHTML = printpage; 
			//打印
			window.print(); 
			//把body的内容还原
			document.body.innerHTML = bodyHtml; 
			return true;

		}
		//点击按钮打印
		$("#lmb-hcDet-print").linkbutton({
			onClick: function(){
				var printpage = $('#lmb-hcDet-printArea').html();
				printdivHC(printpage);
			}
		});*/
		
		//点击按钮打印
		$("#lmb-hcDet-print").linkbutton({
			onClick: function(){
				$("#lmb-hcDet-printArea").jqprint();			
			}
		});
		
		// 刷新
		$("#hc-sx").linkbutton({onClick:function(){
			$("#lmb_hc_tab").datagrid({
	    		url: 'to_cg_findAllCouPur', 
	    		method: 'get'
	    	});
		}});
		
	});
	
</script> 
		<table id="lmb_hc_tab" class="easyui-datagrid" data-options="
			url:'to_cg_findAllCouPur',
			fitColumns:true,fit:true,
			pagination: true,
			pageSize: 5,
			pageList: [5, 10, 15, 20],
			rownumbers: true,
			toolbar: '#lmb—hc',
			border:false
			">
		<thead>
			<tr>
				<th data-options="field:'',checkbox:true"></th>
				<th data-options="field:'cpId',width:'25%'">采购单号</th>
				<th data-options="field:'cpPlId',width:'25%'">计划单号</th>				
				<th data-options="field:'sname',width:'17%'">采购人</th>
				<!-- <th data-options="field:'mname',width:'10%'">订单金额</th> -->
				<th data-options="field:'cpStatus', width:'16%'">验收状态</th>
                <th data-options="field:'cpRsStatus', width:'16%'">是否入库</th>
			</tr>
		</thead>
	</table>
	
	<div id="lmb—hc">
		<a id="hc-add" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true">添加订单</a>
		<a id="hc-xq" class="easyui-linkbutton" data-options="iconCls:'icon-xiangqing',plain:true">订单详情</a>
		<a id="hc-sx" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true">刷新</a>
		<input id="lmb_hc-search" class="easyui-searchbox" data-options="menu: '#lmb-hc-mm', width: 250, prompt: '请输入要查询的内容'"/>
	</div> 
	
	<!-- 搜索栏 -->
	<div id="lmb-hc-mm" style="width:120px">
	    <div data-options="name:'ddbh'">订单编号</div>
	    <div data-options="name:'yszt'">验收状态</div>
	    <div data-options="name:'rkzt'">是否入库</div>
	</div>
	
	<div id="lmb_win_hc-add" class="easyui-window" title="新增采购订单" data-options="collapsible:true,minimizable:false,maximizable:true,modal:true,iconCls:'icon-save',closed: true" style="width:800px;height:320px;padding:5px;">
		<div class="easyui-layout" style="padding:0px;" data-options="fit:true">			
			<div data-options="region:'center'" style="padding:5px;border-top:none;border-left:none;border-right:none;">			
				<form id="lmb_hc" action="to_addCousumablePurchase" method="post">
					<table width="100%" style="background: rgb(236,243,255); padding: 10px; border-radius: 5px;">
						<tr>
							<td width="53%">
								<select id="lmb-coupur-cg" name="cousumablePurchase.cpPlId" class="easyui-combogrid" 
									    data-options="									    
									    label: '采购计划单',
									    labelAlign:'right',							  
									    width:'90%',
									    panelWidth:350,
									    required:true,
									    url:'to_cg_findbytj',
									    queryParams:{
									    	page:1, 
							    			rows:10
									    },
									    pagination: true,
										pageSize: 5,
										pageList: [5, 10, 15, 20],
									    idField:'cpPlanId',
									    textField:'cpPlanId',							    
									    columns:[[
									    {field:'cpPlanId',title:'计划单号',width:89},
									    {field:'cpPlanTitle',title:'名称',width:89},
									    {field:'cpPlanStatus',title:'审批状态',width:84},
									     {field:'cpPlanPurstatus',title:'采购状态',width:84},
									    ]]
								"></select>
							</td>
							<td width="47%">
								<select id="" name="cousumablePurchase.cpBuyer" class="easyui-combogrid" 
									    data-options="
									    required:true,
									    label: '采购人',
									    labelAlign:'right',							  
									    width:'85%',
										panelWidth:288,
										required:true,
									    url: 'queryAllStaff',
										pagination: true,
										pageSize: 8,
										pageList: [8, 16, 24, 32],
										idField: 'staffNo',
										textField: 'staffName',
										columns: [[
											{field:'staffName',title:'姓名',width:100},
											{field:'departmentName',title:'部门',width:100},
											{field:'postName',title:'职务',width:85}
										]]	
								"></select>
							</td>								
						</tr>
						<tr>				
							<td>
								<input id=""class="easyui-datebox" name="cousumablePurchase.cpTime"
								data-options="label:'采购日期',width:'60%',required:true,labelAlign:'right'">
							</td>
						</tr> 
					</table>
					<div >
						<div id="lmb_hc_xq_tab" class="class="easyui-window"" style="width:100%;margin-top:10px;"
								data-options="plain:true,narrow:true">
							<div id="lmb_hc_xq_d" title="采购计划明细" >	
									<table id="lmb_hc_xq" class="easyui-datagrid" style="height:auto"
											data-options="
												iconCls:'icon-edit',
												singleSelect: true,
												toolbar: '#lmb_tb2',
												method: 'get',	
												onClickCell: onClickCell,
												onEndEdit: onEndEdit,		
												width: '100%'
											">
											<thead>
												<tr>
													<th data-options="field:'cpdCId',width:'15%',editor:'textbox',hidden:true">耗材ID</th>
													<th data-options="field:'cpdName',width:'15%',editor:{
														type:'combogrid',
														required:true,
														options:{	
															required:true,						
															url:'to_findConsumable',			
															idField: 'cId',
															textField: 'cName',
															panelWidth: '245',
															panelHeight: 'auto',
															columns: [[
																{field:'cId',title:'耗材编号',width:120},
																{field:'cName',title:'耗材名称',width:120},
															]]
														}
													}">耗材名称</th>
													<th data-options="field:'cpdBatchNum',width:'9%',required:true,editor:{type:'textbox',options:{precision:0,required:true}}">批号</th>
													<th data-options="field:'cpdNorms',width:'9%',editor:{
															type:'combobox',	
															options:{
																required:true,
																valueField:'label',
																textField:'value',
																panelHeight: 'auto',
																data: [{label:'箱',value:'箱'},{label:'盒',value:'盒'}]
															}
														}">单位</th>
													<th data-options="field:'cpdPrice',width:'8%',required:true,editor:{type:'numberbox',options:{precision:2,required:true}}">单价</th>
													<th data-options="field:'cpdNum',width:'8%',required:true,editor:{type:'numberbox',options:{precision:0,required:true}}">数量</th>
													<th data-options="field:'zj',width:'8%',required:true,editor:{type:'numberbox',options:{precision:2,required:true,disabled:true}}">金额</th>
													<th data-options="field:'cpdProductDate',width:'12%',editor:{
														type:'datebox',
														options:{
															required:true,
														}
													}">生产日期</th>
													<th data-options="field:'cpdValidTill',width:'12%',editor:{
														type:'datebox',
														options:{
															required:true,
														}
													}">有效日期</th>
													<th data-options="field:'cpdSupplier',width:'12%',editor:'textbox',hidden:true">供应商ID</th>
													<th data-options="field:'gname',width:'18%',editor:{
															type:'combogrid',
															options:{
																required:true,
																url:'to_lmb_findSupplier',
																required:true,
																idField: 'supId',
																textField: 'supName',
																panelWidth: '257',
																panelHeight: 'auto',
																columns: [[
																{field:'supId',title:'供应商编号',width:125},
																{field:'supName',title:'供应商名称',width:130},
															]]
															}
														}">供应商</th>
												</tr>
											</thead>
										</table>
										<div id="lmb_tb2" style="height:auto">
											<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-ok',plain:true" onclick="accept()">应用</a>
											<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="reject()">撤销</a>
										</div>																				
								</div>										
							</div>							
						</div>	
						<!-- 确认按钮 -->
						<a class="easyui-linkbutton" style="margin-top:10px;algin:right;" data-options="iconCls:'icon-ok',onClick: saveHc">提交</a>		
					</form>	
				<!-- 
					<div data-options="region:'south',border:false" style="text-align:right;margin-left:5px">
					<a class="easyui-linkbutton" data-options="iconCls:'icon-ok',onClick: saveHc">提交订单</a>				
				</div>	
				 -->				
			</div>			
		</div>
	</div>	
	
	<div id="lmb-hcDet-window" class="easyui-window" title="采购详情" data-options="collapsible:false,minimizable:false,maximizable:false,modal:true,iconCls:'icon-save',closed: true" style="width:80%;height:70%;">		
		<div class="easyui-layout" style="padding:0px;" data-options="fit:true">			
			<div id="lmb-hcDet-printArea" data-options="region:'center'" style="padding:5px;border-top:none;border-left:none;border-right:none;">			
				<form id="lmb_hcDet_form" method="post">
					<h1 style="text-align: center">耗材采购记录</h1>
					<table width="100%">
						<tr>				
							<td>采购单号: <a id="lmb-hcDet-cgdh" name="cpId"         class="easyui-linkbutton" data-options="plain:true,"></a></td>
							<td>计划单号: <a id="lmb-hcDet-jhdh" name="cpPlId"  class="easyui-linkbutton" data-options="plain:true,"></a></td>
						</tr>
						<tr>
							<td>采购员: <a id="lmb-hcDet-cgy" name="cpBuyer"       class="easyui-linkbutton" data-options="plain:true"></a></td>					
							<td>采购时间: <a id="lmb-hcDet-cgsj" name="cpTime"         class="easyui-linkbutton" data-options="plain:true,"></a></td>
						</tr>			
					</table>						
					<div style="margin: 5px;">
						<table id="lmb-hcxDet-dg" class="easyui-datagrid" 
							data-options="
								iconCls: 'icon-edit',
								singleSelect: true,										
								width: '100%'						
						">			
							<thead>
								<tr>		
									<th data-options="field:'pdName',width:'18%',align:'center'">耗材名称</th>
									<th data-options="field:'pdBatchNum',width:'14%',align:'center'">批号</th>					
									<th data-options="field:'pdNorms',width:'9%',align:'center'">规格</th>
									<th data-options="field:'pdNum',width:'9%',align:'center'">数量</th>
									<th data-options="field:'pdPrice',width:'9%',align:'center'">单价</th>
									<th data-options="field:'pdProductDate',width:'12%',align:'center',
										formatter:function(value){
				                        if(value){
											var date = new Date(value);  
				                            return date.toLocaleDateString();
										}
									}">生产日期</th>
									<th data-options="field:'pdValidTill',width:'12%',align:'center',
										formatter:function(value){
				                        if(value){
											var date = new Date(value);  
				                            return date.toLocaleDateString();
										}
										
									}">有效期</th>
									<th data-options="field:'supName',width:'18%',align:'center',">供应商</th>
								</tr>
							</thead>
						</table>									
					</div>
				</form>				
			</div>
					
			<div data-options="region:'south',border:false" style="text-align:right;padding:5px 0 0;">
				<a id="lmb-hcDet-print"  class="easyui-linkbutton" data-options="iconCls:'icon-print', plain:true">打印</a>
				<a class="easyui-linkbutton" data-options="iconCls:'icon-cancel', plain:true" href="javascript:$('#lmb-hcDet-window').window('close');">取消</a>
			</div>			
		</div>	
	</div>	
</body>
</html>